package hy;

import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Transaction;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Test1 {
    static Jedis j=new Jedis("192.168.184.100",6379);
    @Test
    public void m1(){
        j.set("address","南京");
        System.out.println(j.get("address"));
        j.incrBy("age",10);
        Set<String> keys= j.keys("*");
        for(String s:keys){
            System.out.println(s+",类型:"+j.type(s));
        }
        Map<String,String> m=new HashMap<>();
        m.put("u1","a1");
        m.put("u2","a2");
        j.hmset("users",m);
        System.out.println(j.hgetAll("users"));
    }
    @Test
    public void t1()throws Exception{
        j.set("money","960");//可用额度
        j.set("debt","40");//欠债
        int consume=10;//本次信用卡消费额度
        j.watch("money");
        Thread.sleep(10000);
        int money=Integer.parseInt(j.get("money"));
        if(money<consume){
            j.unwatch();
            System.out.println("信用卡额度不够");
            return;
        }

        System.out.println("******************事务开启******************");
        Transaction t=j.multi();
        t.incrBy("users",consume);
        t.decrBy("money",consume);
        List<Object>list= t.exec();
        if(list==null){
            System.out.println("事务失败，稍后再试");
        }
        j.unwatch();
    }

    //主从复制
    @Test
    public void ms(){
        Jedis master=new Jedis("192.168.184.100",6379);
        Jedis slave=new Jedis("192.168.184.100",6380);
        //slave.slaveof("192.168.184.100",6379);
        master.set("name1","aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
        System.out.println(slave.get("name1"));
    }
}
